package com.fyzk.adapter.togetherbuy;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.bean.togetherbuy.TogetherHallItem;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;
import com.fyzk.view.DonutProgress;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.quickAdapter.easyRegularAdapter;

import java.util.ArrayList;
import java.util.List;


public class TogetherbuyListAdapter extends easyRegularAdapter<TogetherHallItem,
        TogetherbuyListAdapter.ViewHolder> {


    private Context context;
    private List<TogetherHallItem> infors = new ArrayList<>();


    public TogetherbuyListAdapter(Context context, ArrayList<TogetherHallItem> infors) {
        super(infors);
        this.context = context;
        this.infors = infors;
    }

    @Override
    protected void withBindHolder(final ViewHolder holder1, TogetherHallItem data, int position) {
        final TogetherHallItem info = infors.get(position);
        holder1.lottery_tv.setText(info.getLotteryTypeName());
        holder1.hm_list_safe_tv.setText("保底"+info.getSafePercent()+"%");
        holder1.hm_item_progress.setProgress(info.getBuyPercent());
        holder1.userName_tv.setText(PublicMethod.getSenderName(info.getAlias(),info.getUsername()));
        if (info.getSortState() == 1) {
            holder1.toTop_iv.setVisibility(View.VISIBLE);
        } else {
            holder1.toTop_iv.setVisibility(View.GONE);
        }
        holder1.money_tv.setText(PublicMethod.formatMoney(info.getTotalAmt()) + "米");
        holder1.leftMoney_tv.setText(info.getLeftNum() + "份");
        holder1.persons.setText(info.getParticipantCount() + "人");
        if(onItemClickListener!=null){

            holder1.hm_list_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(info);
                }
            });
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(TogetherHallItem data);
    }


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    class ViewHolder extends UltimateRecyclerviewViewHolder {
        LinearLayout item;
        TextView lottery_tv;
        DonutProgress hm_item_progress;
        TextView userName_tv;
        ImageView toTop_iv;
        TextView money_tv;
        TextView leftMoney_tv;
        TextView hm_list_safe_tv;
        TextView persons;
        LinearLayout hm_list_item;
        public ViewHolder(View convertView, boolean isItem) {
            super(convertView);
            if (isItem) {
                item = convertView.findViewById(R.id.hm_list_item);
                hm_item_progress = convertView.findViewById(R.id.hm_item_progress);
                lottery_tv = convertView.findViewById(R.id.hm_list_lottery_tv);
                userName_tv = convertView.findViewById(R.id.hm_list_username_tv);
                toTop_iv = convertView.findViewById(R.id.hm_list_totop_iv);
                money_tv = convertView.findViewById(R.id.hm_list_money_tv);
                leftMoney_tv = convertView.findViewById(R.id.hm_list_leftmoney_tv);
                persons = convertView.findViewById(R.id.hm_list_persons_tv);
                hm_list_safe_tv = convertView.findViewById(R.id.hm_list_safe_tv);
                hm_list_item = convertView.findViewById(R.id.hm_list_item);
            }
        }
    }

    @Override
    protected int getNormalLayoutResId() {
        return R.layout.hm_dating_listitem;
    }

    @Override
    protected ViewHolder newViewHolder(View view) {
        return new ViewHolder(view, true);
    }

    @Override
    public ViewHolder newFooterHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder newHeaderHolder(View view) {
        return new ViewHolder(view, false);
    }


    @Override
    public int getAdapterItemCount() {
        return infors.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        swapPositions(fromPosition, toPosition);
        super.onItemMove(fromPosition, toPosition);
    }

    @Override
    public void onItemDismiss(int position) {
        if (position > 0)
            removeAt(position);
        super.onItemDismiss(position);
    }
}
